package day01;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/18 20:04
 * 力扣 23 合并多个有序列表
 */
public class PriorityQueuePractice {
    public ListNode mergeKLists(ListNode[] lists) {
        MinHeap heap = new MinHeap(lists.length);
        for (ListNode h : lists) {
            if (h != null) {
                heap.offer(h);
            }
        }
        // 不断从堆顶移除最小元素,加入新链表
        ListNode s = new ListNode(-1, null);
        ListNode t = s;
        while(!heap.isEmpty()) {
            ListNode min = heap.poll();
            t.next = min;
            t = min;

            if (min.next != null) {
                heap.offer(min.next);
            }
        }
        return s.next;
    }
}
